<?php
/**
 * 卡片种类业务层
 * User: Acer
 * Date: 2022/7/8
 * Time: 22:44
 */
namespace app\card\service;

use  app\card\models\CardTypeModel;
class CardTypeService {

    // 新增卡片种类
    public function  s_add_type($data){
        $res = (new CardTypeModel())->insert($data);
        return $res;
    }

    // 修改卡片种类信息
    public function s_modify_card_type($data){
        if(isset($data['ct_id'])){
           $res =  (new CardTypeModel())->save([
                'state'     =>  $data['state'] == 1 ? 1 : 0,
                'card_name' =>  $data['card_name'],
                'type'      =>  $data['type']
            ],[
                'id' =>$data['ct_id']
            ]);
            return $res;
        }
        return false;
    }
    // 获取卡片种类名称
    public function s_card_type_list($page,$paginate=10,$like){
        $CardTypeModel = new CardTypeModel();
        // 卡片种类名称
        if(isset($like['card_name'])){
            $CardTypeModel->whereLike('card_name','%'.$like['card_name']);
        }

        $list = $CardTypeModel->paginate((int)$paginate,false,['page'=>$page]);
        return obj_to_arr($list);
    }
    // 获取卡片种类信息
    public function s_card_type_info($id){
        $CardTypeModel = new CardTypeModel();
        return $CardTypeModel->where('id',$id)->find();
    }

    //修改状态
    public function s_state_card_type($data){
        if(isset($data['ct_id'])){
            // 获取卡片当前状态
            $cardData = (new CardTypeModel())->where(['id' =>$data['ct_id']])->field("state")->find();
            $cardData = obj_to_arr($cardData);
            if(!$cardData)  json_err( -204,['卡片种类信息，']);
            $res =  (new CardTypeModel())->save([
                'state'     =>  $cardData['state'] == 1 ? 0 : 1,
            ],[
                'id' =>$data['ct_id']
            ]);
            return $res;
        }
        return false;
    }

}